package com.ruimei.controller;

import com.alibaba.fastjson.JSONArray;
import com.ruimei.entitys.Attendance;
import com.ruimei.entitys.Teacher;
import com.ruimei.service.AttendanceService;
import com.ruimei.util.Base64Encoder;
import com.ruimei.util.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Api(tags={"考勤"})
@Controller
public class AttendanceController
{
  @Resource
  AttendanceService attendanceService;
  @Resource
  private RedisTemplate<String,String> redisTemplate;
  
  @PostMapping({"/addAttendance"})
  @ResponseBody
  @ApiOperation("W添加考勤信息")
  public Map<String, Object> insertAttendance(Attendance attendance, HttpServletRequest request)
  {
    Teacher teacher = new Teacher();
    teacher.setId(TokenUtils.getTeacherId(request));
    if(redisTemplate.hasKey(teacher.getId())){
      teacher = JSONArray.parseObject(redisTemplate.opsForValue().get(teacher.getId()),Teacher.class);
      Map<String, Object> map = this.attendanceService.insertAttendance(attendance, teacher);
      return map;
    }
    return null;
  }
  
  @ApiOperation("W查询任教班级")
  @GetMapping({"/classgrade/view"})
  @ResponseBody
  public Map<String, Object> getClassGrade(HttpServletRequest request)
  {
    Map<String, Object> map = this.attendanceService.getClassByTeacherId(TokenUtils.getTeacherId(request));
    
    return map;
  }

  @ApiOperation("获取所有班级")
  @GetMapping({"/class"})
  @ResponseBody
  public Map<String, Object> getClassRoom()
  {
    Map<String, Object> map = this.attendanceService.getClassRoom();

    return map;
  }

  
  @ApiOperation(value="W查询学生", notes="通过班级ID")
  @GetMapping({"/Students/{classId}"})
  @ResponseBody
  public Map<String, Object> getStudentsByClassId(@PathVariable("classId") String classId)
  {
    Map<String, Object> map = this.attendanceService.getStudentsByClassId(classId);
    
    return map;
  }
  
  @ApiOperation(value="W通过教师ID查询考勤记录", notes="根据教师ID查询")
  @PostMapping({"/attendance/teacherid"})
  @ResponseBody
  public Map<String, Object> getAttendanceByTeacherId(String attendanceId, String classId, String studentName, String beginTime, String endTime, String attendanceType,HttpServletRequest request)
  {
    Map<String, Object> map = this.attendanceService.getAttendanceByTeacherId(TokenUtils.getTeacherId(request), attendanceId, attendanceType, studentName, classId, beginTime, endTime);
    
    return map;
  }
  @PostMapping({"/attendance/student"})

  @ApiOperation(value="W统计学生考勤", notes="根据班级id,学生姓名,开始时间，结束时间")
  @ResponseBody
  public Map<String, Object> getStudentAttendanceByClassId(String classId, String studentName, String beginTime, String endTime)
  {
    Map<String, Object> map = this.attendanceService.getAttendanceByStudentName(classId, studentName, beginTime, endTime);
    return map;
  }
  
  @PostMapping({"/attendance/parent"})
  @ApiOperation(value="家长查询学生考勤", notes="根据学生id")
  @ResponseBody
  public Map<String, Object> parent(String id, String type, String attendanceId,String beginTime, String endTime)
  {
    Map<String, Object> map = new HashMap();
    map.put("data", this.attendanceService.parentAttendance(id, type, attendanceId,beginTime, endTime));
    map.put("status", Boolean.valueOf(true));
    return map;
  }
}


